package com.ubao.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.ubao.R;
import com.ubao.base.RequestBaseActivity;
import com.ubao.bean.Commodity;
import com.ubao.bean.MyProductList;
import com.ubao.global.GoBackable;
import com.ubao.network.JsonParser;
import com.ubao.network.ResponseInterface;
import com.ubao.network.Url;
import com.ubao.network.req.SpecialRequest;
import com.ubao.network.resp.SpecialResponse;
import com.ubao.ui.titlebar.GTitleBar;
import com.ubao.util.log.Logger;
import com.youxilua.waterfall.WaterFallOption;
import com.youxilua.waterfall.WaterFallView;
import com.youxilua.waterfall.WaterFallView.OnScrollListener;
import com.youxilua.waterfall.item.FlowView;
import com.youxilua.waterfall.item.FlowViewHandler;
import com.youxilua.waterfall.mock.ImageMock;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.widget.LinearLayout;

public class MyWantsActivity extends RequestBaseActivity implements OnScrollListener,GoBackable {
	private WaterFallView waterfall_scroll;
	private LinearLayout waterfall_container;
	private Display display;
	private AssetManager asset_manager;
	private List<String> image_filenames;
	private final String image_path = "images";
	private Handler handler;
	private int item_width;
	private int column_count = 3;// 显示列数
	private GTitleBar gTitleBar;
	private int pagesize = 20;
	private int pageindex = 1;
	private List<Commodity> listComoddity = new ArrayList<Commodity>();
	WaterFallOption fallOption;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ui_mywants);
		display = getWindowManager().getDefaultDisplay();
		item_width = display.getWidth() / column_count;// 根据屏幕大小计算每列大小
		asset_manager = getAssets();

		initLayout();
	}

	private void initLayout() {
		initTitleBar();
		
		//1 初始化waterfall 
		waterfall_scroll = (WaterFallView) findViewById(R.id.waterfall_scroll);
		//2 初始化显示容器
		waterfall_container = (LinearLayout) findViewById(R.id.waterfall_container);
		//3,设置滚动监听
		waterfall_scroll.setOnScrollListener(this);
		//4,实例一个设置
		fallOption = new WaterFallOption(waterfall_container,
				item_width, column_count);
		//5,提交更改,实现android瀑布流
		waterfall_scroll.commitWaterFall(fallOption, waterfall_scroll);
		
		
		//1,瀑布流的item 处理设置
		handler = new FlowViewHandler(waterfall_scroll);

		// 加载所有图片路径
		
		// 第一次加载
		AddItemToContainer(waterfall_scroll.current_page,
				waterfall_scroll.pageCount);
		sendMyProductRequest();
	}

	private void initTitleBar() {
		// TODO Auto-generated method stub
		gTitleBar = (GTitleBar) findViewById(R.id.mTitleBar);
		gTitleBar.initTitle("我要淘货");
		gTitleBar.initLeftButton("", R.drawable.titlebar_menu, new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MyApp.slidingMenuView.doScroll();
			}
		});
		gTitleBar.initRightButton("", R.drawable.titlebar_addfriend, new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	String typeid = "1";
	String distance = "0";
	private void sendMyProductRequest() {
		SpecialRequest request = new SpecialRequest(Url.url_commoditylist_full);
		request.postParams = Url.getCommoditylist(this,String.valueOf(pagesize), String.valueOf(pageindex),typeid,distance,MyApp.locationManager.longtitude,MyApp.locationManager.latitude);
		mHttpHandler.sendRequest(request);
	}

	private void AddItemToContainer(int pageindex, int pagecount) {

		int currentIndex = pageindex * pagecount;
		int imagecount = waterfall_scroll.pictureTotalCount;// image_filenames.size();
		
//		for (int i = currentIndex; i < pagecount * (pageindex + 1)
//				&& i < imagecount; i++) {
//			waterfall_scroll.loaded_count++;
//			Random rand = new Random();
//			int r = rand.nextInt(ImageMock.imageThumbUrls.length);
//			AddImage(ImageMock.imageThumbUrls[r],
//					(int) Math.ceil(waterfall_scroll.loaded_count / (double) column_count),
//					waterfall_scroll.loaded_count);
//		}
		
		
		Logger.d("pageindex:" + pageindex + " pagecount:" + pagecount + " imagecount:" + imagecount + " " + (pagecount * (pageindex + 1)));
		for (int i = currentIndex; i < pagecount * (pageindex + 1)&& i < imagecount; i++) {
			waterfall_scroll.loaded_count++;
			AddImage(listComoddity.get(i),
					(int) Math.ceil(waterfall_scroll.loaded_count / (double) column_count),
					waterfall_scroll.loaded_count);
		}

	}

	private void AddImage(Commodity commodity, int rowIndex, int id) {

		FlowView item = new FlowView(this);
		item.setPadding(1, 1, 1, 1);
		item.setRowIndex(rowIndex);
		item.setId(id);
		item.setViewHandler(handler);
		item.setCommodity(commodity);
		item.setItemWidth(item_width);
		item.LoadImage();

	}

	// waterfall 监听

	@Override
	public void onBottom() {
		AddItemToContainer(++(waterfall_scroll.current_page), waterfall_scroll.pageCount);
	}

	@Override
	public void onTop() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onAutoScroll(int l, int t, int oldl, int oldt) {
		// TODO Auto-generated method stub

	}
	int totalCount;
	@Override
	protected void httpCompleted(ResponseInterface resp) {
		// TODO Auto-generated method stub
		SpecialResponse response = (SpecialResponse)resp;
		MyProductList list = JsonParser.getCommoditylist(response.content);
		if(!list.getStatus().equals("200")){
			Message msg = refreshHandler.obtainMessage();
			msg.obj = list.getMessage();
			msg.what = 0;
			refreshHandler.sendMessage(msg);
		}else{
			totalCount = Integer.parseInt(list.getRecordCount());
			listComoddity.addAll(list.getRecords());
			refreshHandler.sendEmptyMessage(1);
		}
	}
	
	Handler refreshHandler =new Handler(){
		public void handleMessage(android.os.Message msg) {
			closeProgress();
			switch(msg.what){
			case 0:
				showToast((String)msg.obj);
				break;
			case 1:
				totalCount = waterfall_scroll.pictureTotalCount + listComoddity.size();
				waterfall_scroll.pictureTotalCount = totalCount;
				fallOption.pictureTotalCount = totalCount;
				AddItemToContainer(waterfall_scroll.current_page,waterfall_scroll.pageCount);
				break;
			}
		};
	};

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void getIntentData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void setIntentData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setGoBack() {
		// TODO Auto-generated method stub
		
	}
}
